package com.MonkWorks.timedormitory;

import com.MonkWorks.timedormitory.R;
import com.MonkWorks.timedormitoryDB.IUserDB;
import com.MonkWorks.timedormitoryDB.ItimeProvider;
import com.MonkWorks.timedormitoryDB.TimeDB;
import com.MonkWorks.timedormitoryDB.UserDBStub;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class LoginActivity extends Activity {
	// bringing in IUser services --to be deleted
	IUserDB userDB;
	// replaces old IUserDB, brings in ItimeProvider
	ItimeProvider timeUser;
	// Declare our Views, so we can access them later
	private EditText etUsername;
	private EditText etPassword;
	private Button button_Login;
	private String client;

	private void initComponents() {
		// implement the timeDB
		timeUser = new TimeDB(this);
		// Get the EditText and Button References
		etUsername = (EditText) findViewById(R.id.etfUser);
		etPassword = (EditText) findViewById(R.id.etfPassword);
		button_Login = (Button) findViewById(R.id.btnLogin);

		button_Login.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				Intent MIntent = new Intent(v.getContext(),
						AdminToolsActivity.class);
				Intent TIntent = new Intent(v.getContext(), PunchActivity.class);
				// Check Login
				String username = etUsername.getText().toString();
				String password = etPassword.getText().toString();

				// Begin Logon verificaiton -Chavez
				// admin account username= Admin password= 1234
				try {
					// new logon method
					if (timeUser.Logon(username, password) == true) {
						popup("Login successful.");
						// if username is admin, the admin display is opened
						if (username.equals("Admin")) {
							startActivityForResult(MIntent, 0);
							finish();
						} else {
							// this screen is displayed for normal users
							// Pushes the username to the punch activity
							TIntent.putExtra("username", username);
							startActivity(TIntent);
							finish();
						}

					} else {
						// if login is not successful, this will be displayed
						popup("Invalid Username/password");
					}
				} catch (Exception e) {
					// if logon method doesn't work, this message will be thrown
					Log.e("Login Method", "LoginActivity");
					popup("Something went wrong");
				}

				// if login is correct send successful popup and send to clock
				// in/ out page
				/*
				 * if(username.equals("admin") && password.equals("admin")){
				 * popup("Login successful."); startActivityForResult(MIntent,
				 * 0); finish(); } else if(username.equals("user") &&
				 * password.equals("user")){ popup("Login successful.");
				 * startActivityForResult(TIntent,0); }else{ popup(
				 * "Login unsuccessful because username or password not correct"
				 * ); } //This throws two popup messages no matter what, we need
				 * to re-think this. Try/Catch possibly? // Commented out to
				 * implement the DB services
				 */

			}
		});

	}

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_login);
		initComponents();
		// This is a hardcoded admin account method that is suppose to create an
		// admin account if it doesn't already exsist
		try {
			timeUser.generateAdmin();
		} catch (Exception e) {
			Log.e("LoginActivity", "generate Admin");
			popup("No admin account was created");
		}
	}

	protected void popup(String message) {
		// this is how we make a popup.
		Toast.makeText(this, message, Toast.LENGTH_LONG).show();
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.activity_login, menu);
		return true;
	}
}